package com.cet.billingjob.jobs;

import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

/**
 * @author 30952
 */
public class FilePreparationTasklet implements Tasklet {

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
        JobParameters jobParameters = contribution.getStepExecution().getJobParameters();
//        System.out.println(new File(".").getCanonicalFile());
        String inputFile = jobParameters.getString("input.file");
        assert inputFile != null;
        Path source = Paths.get(inputFile);

        // staging dir
        // D:\projects\cet-practice\spirng-test\batch\billing-job\input\billing-2023-03.csv
        // D:\projects\cet-practice\spirng-test\batch\billing-job\staging\billing-2023-03.csv

        // input/billing-2023-03.csv -> staging/billing-2023-03.csv
        Path grandParentPath = source.getParent().getParent();
        Path target = Paths.get(grandParentPath == null ? "" : grandParentPath.toString(), "staging", source.toFile().getName());
        Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
        return RepeatStatus.FINISHED;
    }
}
